package web.servlet;

import domain.Order;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

public class ShippingFormServlet extends HttpServlet {
    //实现跳转到confirm页面,实现shippingForm中的值的修改的保存

    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Order order = (Order) session.getAttribute("currentOrder");

        //获取页面中的值
        String shipToFirstName = req.getParameter("shipToFirstName");
        String shipToLastName = req.getParameter("shipToLastName");
        String shipAddress1 = req.getParameter("shipAddress1");
        String shipAddress2 = req.getParameter("shipAddress2");
        String shipCity = req.getParameter("shipCity");
        String shipState = req.getParameter("shipState");
        String shipZip = req.getParameter("shipZip");
        String shipCountry = req.getParameter("shipCountry");

        if (shipToFirstName != null && !shipToFirstName.equals("")){
            order.setShipToFirstName(shipToFirstName);
        }
        if (shipToLastName != null && !shipToLastName.equals("")){
            order.setShipToLastName(shipToLastName);
        }
        if (shipAddress1 != null && !shipAddress1.equals("")){
            order.setShipAddress1(shipAddress1);
        }
        if (shipAddress2 != null && !shipAddress2.equals("")){
            order.setShipAddress2(shipAddress2);
        }
        if (shipCity != null && !shipCity.equals("")){
            order.setShipCity(shipCity);
        }
        if (shipState != null && !shipState.equals("")){
            order.setShipState(shipState);
        }
        if (shipZip != null && !shipZip.equals("")){
            order.setShipZip(shipZip);
        }
        if (shipCountry != null && !shipCountry.equals("")){
            order.setShipCountry(shipCountry);
        }
        session.setAttribute("currentOrder",order);
        PrintWriter out = resp.getWriter();
        out.print("shippingInfoSaved");
    //        req.getRequestDispatcher(CONFIRM_ORDER).forward(req,resp);
    }
}
